package com.novel.test;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.jsoup.Connection;
import org.jsoup.Connection.Method;
import org.jsoup.Connection.Request;
import org.jsoup.Connection.Response;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;

import kong.unirest.Headers;
import kong.unirest.HttpResponse;
import kong.unirest.JsonNode;
import kong.unirest.Unirest;

public class GetNovelRecharge {
	//请求头，带个user-agent，connection
	public static String CONNECTION = "Connection";
	public static String CONNECTION_VAL = "keep-alive";
	public static String USER_AGENT = "User-Agent";
	public static String USER_AGENT_VAL = "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:67.0) Gecko/20100101 Firefox/67.0";
	
	//cookie,
	public static Map<String, String> cookies = new HashMap<>();
	
	//登录数据,
	public static Map<String, String> loginData = new HashMap<>();
	public static Map<String, Object> loginData2 = new HashMap<>();
	
	//headers，使用请求头组装,
	public static Map<String, String> headers = new HashMap<>();
	
	static{
		headers.put(CONNECTION, CONNECTION_VAL);
		headers.put(USER_AGENT, USER_AGENT_VAL);
	}
	
	public static void main(String[] args) throws IOException {
		
		weixiaobao();
	}
	
	private static void test() throws IOException {
		String url = "https://api-xs.wxb.com/novel/popularizeNovel?page=1&pagesize=15&type=30";
		Connection con = Jsoup.connect(url).timeout(7000).ignoreHttpErrors(true); 
		
		headers.put("Host","api-xs.wxb.com");
		headers.put("User-Agent","Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:67.0) Gecko/20100101 Firefox/67.0");
		headers.put("Accept","application/json, text/plain, */*");
		headers.put("Accept-Language","zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2");
		headers.put("Accept-Encodinge","gzip, deflate, br");
		headers.put("Referer","https://xs.wxb.com/center/stat");
		headers.put("X-Requested-With","XMLHttpRequest");
		headers.put("Origin","https://xs.wxb.com");
		headers.put("Connection","keep-alive");
		headers.put("TE","Trailers");
		
		cookies.put("PHPSESSID","aa73e4c3c814c4608e02dc08e1e44ebd");
		cookies.put("Hm_lvt_5859c7e2fd49a1739a0b0f5a28532d91","1565149141");
		cookies.put("Hm_lpvt_5859c7e2fd49a1739a0b0f5a28532d91","1565338908");
		cookies.put("RMU","1618821");
		cookies.put("RMT","60cb232b31a8cdb928f83100c54b74fd");
		
		con.headers(headers);
		con.cookies(cookies);
		Response res = con.ignoreContentType(true).execute();
		String body = res.body();
		Map<String, String> cookie = res.cookies();
		System.out.println(body);
	}
	
	
	private static void weixiaobao() throws IOException {
		String email = "15802541812";  
        String password = "xgmedia.123"; 
        String remember = "on"; 
        String captcha = ""; 
        String url = "https://account.wxb.com/index2/login"; 
        
        //忽略证书
        HttpsUrlValidator.retrieveResponseFromServer(url); 
        
        Connection conn = getConn(url);  
        Response res = conn.execute();
        Document doc = res.parse();
        
        Map<String, String> cookie = res.cookies();
        cookies.putAll(cookie);
        
        loginData.put("email", email);  
        loginData.put("password", password);  
        loginData.put("captcha", captcha);  
        loginData.put("from", "https://xs.wxb.com/index");  
        loginData.put("remember", remember);
        
        conn = getConn(url); 
        
        conn = conn.header("Accept", "application/json, text/plain, */*")
		.header("Content-Type", "application/x-www-form-urlencoded")
		.header("X-Requested-With", "XMLHttpRequest")
		.ignoreContentType(true);
        
        login(conn);
        
        url = "https://api-xs.wxb.com/stat/list?page=1&pagesize=20";
        conn = getConn(url);  
        res = conn.method(Method.GET).headers(headers)
        		.header("Host", "api-xs.wxb.com")
        		.header("Accept", "application/json, text/plain, */*")
        		.header("Accept-Language", "zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2")
        		.header("Accept-Encoding", "gzip, deflate, br")
        		.header("Referer", "https://xs.wxb.com/collect")
        		.header("Origin", "https://xs.wxb.com")
        		.cookies(cookies)
        		.header("X-Requested-With", "XMLHttpRequest")
        		.ignoreContentType(true)
        		.execute();
        String resBody = res.body();
        System.out.println(resBody);
        
	}
	
	
	private static void yifengaf() throws IOException {
		String userName = "yixing";  
		String password = "xuedian123"; 
		String url = "https://admin.yifengaf.cn/admin/index/login?url=/admin/notice/index?ref=addtabs"; 
		
		//忽略证书
		HttpsUrlValidator.retrieveResponseFromServer(url); 
		
		Connection conn = getConn(url);  
		Response res = conn.execute();
		Document doc = res.parse();
		
		Map<String, String> cookie = res.cookies();
		cookies.putAll(cookie);
		
		String __token__ = doc.select("input[name=__token__]").val();
		
		loginData.put("username", userName);  
		loginData.put("password", password);  
		loginData.put("__token__", __token__);  
		loginData.put("keeplogin", "1");  
		
		login(conn);
		
		url = "https://admin.yifengaf.cn/admin/book/collect/index?sort=recharge_money&order=desc&offset=0&limit=10&_="+System.currentTimeMillis();
		
		//忽略证书
		HttpsUrlValidator.retrieveResponseFromServer(url); 
		
		conn = getConn(url);  
		res = conn.method(Method.GET).headers(headers)
				.header("Accept", "application/json, text/javascript, */*; q=0.01")
				.header("Content-Type", "application/json")
				.cookies(cookies)
				.header("X-Requested-With", "XMLHttpRequest")
				.ignoreContentType(true)
				.execute();
		String resBody = res.body();
		System.out.println(resBody);
		
	}
	
	

	private static Connection getConn(String url) {
		Connection conSearch = Jsoup.connect(url).timeout(7000).ignoreHttpErrors(true);  
		conSearch.headers(headers);
		conSearch.cookies(cookies);
		return conSearch;
	}

	private static void login( Connection conn) throws IOException {
        //登录地址  
        conn.data(loginData).cookies(cookies);  
        Response response = conn.followRedirects(true).method(Method.POST).execute();  
        Document doc = response.parse();
        Map<String, String> cookie = response.cookies();  
        cookies.putAll(cookie);
	}
	
	private static void login2() throws IOException {
		Unirest.config()
        .socketTimeout(500)
        .connectTimeout(1000)
        .concurrency(10, 5)
        .setDefaultHeader("Accept", "application/json, text/javascript, */*; q=0.01")
        .followRedirects(false);
		
		HttpResponse<String> response = Unirest.post("https://admin.yifengaf.cn/admin/index/login")
			      .headers(headers)
			      .fields(loginData2)
			      .asString();
		String body = response.getBody();
		Headers h  = response.getHeaders();
		System.out.println(h.size());
		System.out.println(h.get("Set-Cookie"));
		
		System.out.println(body);
		Unirest.shutDown();
	}
}
